import axios from 'axios';

//用户名登录
function login(uname, upwd) {
  return new Promise((resolve, reject) => {
    axios.post(`/login`, `uname=${uname}&upwd=${upwd}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.token);
      else if (result.data.ok == -2) reject(result.data.ok);
      else if (result.data.ok == -3) reject(result.data.ok);
      else reject();
    });
  });
}

//发送短信验证码
function SMSverification(phone) {
  return new Promise((resolve, reject) => {
    axios.get(`/msgLogin/${phone}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.code);
      else reject();
    });
  });
}

// 短信登录
function msgLogin(phone, code) {
  return new Promise((resolve, reject) => {
    axios.post(`/msgLogin`, `phone=${phone}&code=${code}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.token);
      else reject(result.data.ok);
    });
  });
}

// 发送邮箱验证码
function sendEmailCode(email) {
  return new Promise((resolve, reject) => {
    axios.post(`/emailCode`, `email=${email}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.emailCode);
      else reject();
    });
  });
}

//注册
function register(phone, uname, upwd, email, emailCode) {
  return new Promise((resolve, reject) => {
    axios.post(`/register`, `phone=${phone}&uname=${uname}&upwd=${upwd}&email=${email}&emailCode=${emailCode}`).then((result) => {
      if (result.data.ok == 1) resolve();
      else reject(result.data.ok);
    });
  });
}

// 注销
function deleteUser(token) {
  return new Promise((resolve, reject) => {
    axios.put(`/deleteUser`, `token=${token}`).then((result) => {
      if (result.data.ok == 1) resolve();
      else reject();
    });
  });
}

// 获取用户信息
function getUserInfo(token) {
  return new Promise((resolve, reject) => {
    axios.post(`/getUserInfo`, `token=${token}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.result);
      else reject();
    });
  });
}

// 修改用户信息
function updateUserInfo(token, form) {
  return new Promise((resolve, reject) => {
    axios.put(`/updateUserInfo`, `token=${token}&phone=${form.phone}&uname=${form.uname}&autograph=${form.autograph}&avatar=${form.avatar}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.ok);
      else if (result.data.ok == -1) resolve(result.data.ok);
      else reject();
    });
  });
}

// 修改用户密码
function updatePassword(token, oldPwd, newPwd) {
  return new Promise((resolve, reject) => {
    axios.put(`/updatePassword`, `token=${token}&oldPwd=${oldPwd}&newPwd=${newPwd}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.ok);
      else if (result.data.ok == -1) resolve(result.data.ok);
      else if (result.data.ok == -2) resolve(result.data.ok);
      else reject();
    });
  });
}

// 获取旅行订单
function getOrder(token) {
  return new Promise((resolve, reject) => {
    axios.post(`/getOrder`, `token=${token}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.result);
      else reject();
    });
  });
}

// 获取订制订单
function getCustom(token) {
  return new Promise((resolve, reject) => {
    axios.post(`/getCustom`, `token=${token}`).then((result) => {
      if (result.data.ok == 1) resolve(result.data.result);
      else reject();
    });
  });
}

export { deleteUser, SMSverification, sendEmailCode, msgLogin, register, login, getUserInfo, updateUserInfo, updatePassword, getOrder, getCustom };
